﻿Imports System.Data.OleDb
Imports System.Data
Imports System.Configuration
Imports System.Drawing
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class Photodetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hiddenUserName.Value = Context.User.Identity.Name
        If Not IsPostBack Then
            BindRatingControl()
        End If
       
    End Sub
    Protected Sub bindrepeater()
        Dim item As ListViewItem
        item = ListView2.Items(0)
        Dim ds1 As SqlDataSource = DirectCast(item.FindControl("SqlDataSource5"), SqlDataSource)
        Dim ds2 As SqlDataSource = DirectCast(item.FindControl("SqlDataSource2"), SqlDataSource)
        Dim ds3 As SqlDataSource = DirectCast(item.FindControl("SqlDataSource4"), SqlDataSource)

        ds1.DataBind()
        ds2.DataBind()
        ds3.DataBind()
    End Sub
    Protected Sub RatingControlChanged(ByVal sender As Object, ByVal e As AjaxControlToolkit.RatingEventArgs) Handles Rating1.Changed
        Dim myphotoID As Integer = 0
        If Not Request.QueryString("PhotoID") Is Nothing Then
            myphotoID = CInt(Request.Params("PhotoID"))
        End If

        Dim tusername As String = hiddenUserName.Value
        If myphotoID <> 0 Then
            Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
            con.Open()
            Dim cmd As New OleDbCommand("insert into tblPhotoRating (PhotoID, Username, Ratings)values(@PhotoID,@Username,@Rating)", con)
            cmd.Parameters.AddWithValue("@PhotoID", myphotoID)
            cmd.Parameters.AddWithValue("@Username", tusername)
            cmd.Parameters.AddWithValue("@Rating", Rating1.CurrentRating)
            cmd.ExecuteNonQuery()
            con.Close()
        End If
        BindRatingControl()
    End Sub
    Protected Sub BindRatingControl()
        Dim myphotoID As Integer = 0
        If Not Request.QueryString("PhotoID") Is Nothing Then
            myphotoID = CInt(Request.Params("PhotoID"))
        End If
        Dim total As Integer = 0
        Dim dt As New DataTable()
        Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
        con.Open()
        Dim cmd As New OleDbCommand("SELECT Ratings AS RTNG FROM tblPhotoRating WHERE (PhotoID = @PhotoID)", con)
        cmd.Parameters.AddWithValue("@PhotoID", myphotoID)
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                total += Convert.ToInt32(dt.Rows(i)(0).ToString())
            Next
            Dim average As Integer = total \ (dt.Rows.Count)
            Rating1.CurrentRating = average
        End If
        If dt.Rows.Count = 1 Then
            lbltxt.Text = dt.Rows.Count & " user has rated this Picture."
        End If

        If dt.Rows.Count > 1 Then
            lbltxt.Text = dt.Rows.Count & " users have rated this Picture."
        End If
        If dt.Rows.Count = 0 Then
            lbltxt.Text = "No  user have rated this Picture."
        End If
        con.Close()
    End Sub
    Public Function ConvertDataTabletoString() As String

        Dim myphotoID As Integer = 0
        If Not Request.QueryString("PhotoID") Is Nothing Then
            myphotoID = CInt(Request.Params("PhotoID"))
        End If
        Dim dt As New DataTable()
        Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
        Dim cmd As New OleDbCommand("SELECT LatLoc as lat, LongLoc as lng FROM tblPhoto WHERE (((tblPhoto.PhotoID)=[@PhotoID]));", con)
        cmd.Parameters.AddWithValue("@PhotoID", myphotoID)
        con.Open()
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(dt)
        Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim rows As New List(Of Dictionary(Of String, Object))()
        Dim row As Dictionary(Of String, Object)
        For Each dr As DataRow In dt.Rows
            row = New Dictionary(Of String, Object)()
            For Each col As DataColumn In dt.Columns
                row.Add(col.ColumnName, dr(col))
            Next
            rows.Add(row)
        Next
        Return serializer.Serialize(rows)

    End Function

    

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim tusername As String = Context.User.Identity.Name
        Dim myphotoID As Integer = 0
        If Not Request.QueryString("PhotoID") Is Nothing Then
            myphotoID = CInt(Request.Params("PhotoID"))

            Dim dt As New DataTable()
            Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
            con.Open()
            Dim cmd As New OleDbCommand("SELECT tblPhotoLike.Username FROM tblPhotoLike WHERE tblPhotoLike.PhotoID = @PhotoID and tblPhotoLike.Username = @Username;", con)
            cmd.Parameters.AddWithValue("@PhotoID", myphotoID)
            cmd.Parameters.AddWithValue("@Username", tusername)
            Dim da As New OleDbDataAdapter(cmd)
            da.Fill(dt)
            If dt.Rows.Count = 0 Then
                Dim inscmd As New OleDbCommand("Insert into tblPhotoLike (PhotoID, Username) Values (@PhotoID, @Username);", con)
                inscmd.Parameters.AddWithValue("@PhotoID", myphotoID)
                inscmd.Parameters.AddWithValue("@Username", tusername)
                inscmd.ExecuteNonQuery()
                Response.Redirect("~/photodetail.aspx?PhotoID=" + CStr(myphotoID))
            End If
            con.Close()
        End If
        Response.Redirect("~/photodetail.aspx?PhotoID=" + CStr(myphotoID))
    End Sub

    Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim myphotoID As Integer = 0
        If Not Request.QueryString("PhotoID") Is Nothing Then
            myphotoID = CInt(Request.Params("PhotoID"))
        End If
        Dim tusername As String = hiddenUserName.Value
        If myphotoID <> 0 And txtComment.Text <> "" Then
            Dim dbdate As DateTime
            Dim dbtime As TimeSpan
            dbdate = Now.Date
            dbtime = Now.TimeOfDay
            Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("fishconn").ConnectionString)
            con.Open()
            Dim cmd As New OleDbCommand("insert into tblPhotoComments (PhotoID, Username, Comments, CommDate, CommmTime) values(@PhotoID, @Username, @Comments, @CommDate, @CommmTim)", con)
            cmd.Parameters.AddWithValue("@PhotoID", myphotoID)
            cmd.Parameters.AddWithValue("@Username", tusername)
            cmd.Parameters.AddWithValue("@Comments", txtComment.Text)
            cmd.Parameters.AddWithValue("@CommDate", dbdate)
            cmd.Parameters.AddWithValue("@CommmTim", dbtime)
            cmd.ExecuteNonQuery()
            con.Close()
            bindrepeater()
            Response.Redirect("~/photodetail.aspx?PhotoID=" + CStr(myphotoID))
        End If

    End Sub

    Protected Sub btnComment_Click(sender As Object, e As EventArgs) Handles btnComment.Click
        Me.ModalPopupExtender1.Show()
    End Sub
End Class